package com.termine.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.termine.db.Connector;
import com.termine.entity.Location;
import com.termine.entity.PersonalData;

@ManagedBean
@ApplicationScoped
public class LocationDAO {

	public void deleteLocation(Integer locationId) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		String statementString = "delete from LOCATION where L_ID = ?";
		try {
			connection = Connector.getConnection();
			statement = connection.prepareStatement(statementString);
			statement.setInt(1, locationId);
			statement.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			if (statement != null) {
				statement.close();
			}
			Connector.closeConnection(connection);
		}
	}
	
	public Location getLocation(PersonalData personalData) throws Exception {
		Location result = null;
		Connection connection = null;
		PreparedStatement statement = null;
		String statementString = "select * from LOCATION where L_ID = ?";   
		try {
			connection = Connector.getConnection();
			statement = connection.prepareStatement(statementString);
			statement.setInt(1, personalData.getLocation_id());
			ResultSet resultSet = statement.executeQuery();
			
			Integer integer;
			String string;
			result = new Location();
			resultSet.next();
			integer = resultSet.getInt("L_ID");
			if (!resultSet.wasNull()) {
				result.setLocation_id(integer);
			}
			string = resultSet.getString("L_COUNTRY");
			if (!resultSet.wasNull()) {
				result.setCountry(string);
			}
			string = resultSet.getString("L_CITY");
			if (!resultSet.wasNull()) {
				result.setCity(string);
			}
			string = resultSet.getString("L_ZIP");
			if (!resultSet.wasNull()) {
				result.setZip(string);
			}
			string = resultSet.getString("L_STREET");
			if (!resultSet.wasNull()) {
				result.setStreet(string);
			}
			return result;
		} catch (Exception e) {
			throw e;
		} finally {
			if (statement != null) {
				statement.close();
			}
			Connector.closeConnection(connection);
		}
	}
	
	public Integer getLocationId(Location location) throws Exception {
		Integer result = null;
		Connection connection = null;
		PreparedStatement statement = null;
		String statementString = "select L_ID from LOCATION where L_COUNTRY = ? and L_CITY = ? and L_ZIP = ? and L_STREET = ?";
		try {
			connection = Connector.getConnection();
			statement = connection.prepareStatement(statementString);
			statement.setString(1, location.getCountry());
			statement.setString(2, location.getCity());
			statement.setString(3, location.getZip());
			statement.setString(4, location.getStreet());
			ResultSet resultSet = statement.executeQuery();
			resultSet.next();
			result = new Integer(resultSet.getInt("L_ID"));
			return result;
		} catch (Exception e) {
			throw e;
		} finally {
			if (statement != null) {
				statement.close();
			}
			Connector.closeConnection(connection);
		}
	}

	public void addLocation(Location location) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		String statementString = "insert into LOCATION (L_COUNTRY, L_CITY, L_ZIP, L_STREET) values (?,?,?,?)";
		try {
			connection = Connector.getConnection();
			statement = connection.prepareStatement(statementString);
			statement.setString(1, location.getCountry());
			statement.setString(2, location.getCity());
			statement.setString(3, location.getZip());
			statement.setString(4, location.getStreet());
			statement.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			if (statement != null) {
				statement.close();
			}
			Connector.closeConnection(connection);
		}
	}

	public void updateLocationForUser(Location location,
			PersonalData personalData) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		String statementString = "update LOCATION set L_COUNTRY = ?, L_CITY = ?, L_ZIP = ?, L_STREET = ? "
				+ "where L_ID = ?";
		try {
			connection = Connector.getConnection();
			statement = connection.prepareStatement(statementString);
			statement.setString(1, location.getCountry());
			statement.setString(2, location.getCity());
			statement.setString(3, location.getZip());
			statement.setString(4, location.getStreet());
			statement.setInt(5, personalData.getLocation_id());
			statement.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			if (statement != null) {
				statement.close();
			}
			Connector.closeConnection(connection);
		}
	}

}
